package com.xingkeduo.business.user.service;

import com.xingkeduo.business.user.dao.JobseekerAssessRateMapper;
import com.xingkeduo.dao.database.DatabaseAccessObject;
import com.xingkeduo.pojo.query.JobseekerAssessQO;
import com.xingkeduo.pojo.query.JobseekerAssessRateQO;
import com.xingkeduo.pojo.table.JobseekerAssessRateTO;
import com.xingkeduo.pojo.table.JobseekerAssessTO;
import com.xingkeduo.pojo.table.JobseekerAssessTO.Type;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.CommonUtil;
import com.xingkeduo.util.tool.IdentityUtil;
import com.xingkeduo.util.tool.IdentityUtil.Worker;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 求职者考核信息类
 *
 * @author 高杰
 * @time 2018年12月26日 上午11:47:39
 * @description
 */
@Component
public class JobseekerAssessService {
    private static final String NAMESPACE = CommonUtil.formatNamespace(JobseekerAssessTO.class);
    private static final String NAMESPACE_RATE = CommonUtil.formatNamespace(JobseekerAssessRateTO.class);
    @Resource(name = "mysqlUserMasterDao")
    private DatabaseAccessObject<JobseekerAssessTO, JobseekerAssessQO> dao;
    @Resource(name = "mysqlUserMasterDao")
    private DatabaseAccessObject<JobseekerAssessRateTO, JobseekerAssessRateQO> rateDao;

    @Resource
    private JobseekerAssessRateMapper jobseekerAssessRateMapper;


    public List<Long> selectUnionAll(JobseekerAssessRateQO jobseekerAssessRateQO,List<Long> assessIds) {
        List<Long> rateIds = jobseekerAssessRateMapper.selectUnionAll(jobseekerAssessRateQO,assessIds);
        return rateIds;
    }

    public List<JobseekerAssessRateTO> selectQuery(List<Long> ids) throws Throwable {
        return jobseekerAssessRateMapper.selectByIds(new JobseekerAssessRateQO().setIds(ids));
    }


    /**
     * 查询
     *
     * @param code
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年1月2日 下午4:35:01
     */
    public List<JobseekerAssessTO> selectByLogin(String login) throws Throwable {
        List<JobseekerAssessTO> assesses = dao.select(NAMESPACE, new JobseekerAssessQO().setLogin(login));
        for (Iterator<JobseekerAssessTO> iterator = assesses.iterator(); iterator.hasNext(); ) {
            JobseekerAssessTO assess = iterator.next();
            assess.setRates(
                    rateDao.select(NAMESPACE_RATE, new JobseekerAssessRateQO().setAssessId(assess.getId())));
        }
        return assesses;
    }

    public List<Long> selectRateListByTypeAndScoreIds(String login) throws Throwable {
        Set<JobseekerAssessTO.Type> set = new HashSet<>(4);
        set.add(Type.TEST1);
        set.add(Type.TEST2);
        List<Long> assessesIds = dao.selectIndex(NAMESPACE, new JobseekerAssessQO().setLogin(login).setTypes(set).setRecordStart(0).setRecordEnd(2));
        return assessesIds;
    }

    /**
     * 获取分数集合
     *
     * @param assesses:
     * @return : java.util.List<com.xingkeduo.pojo.table.JobseekerAssessRateTO>
     * @author : gaozifeng
     * @date : 2019/8/8 16:23
     */
    public List<JobseekerAssessRateTO> rateList(List<JobseekerAssessTO> assesses) throws Throwable {
        List<Long> assessIds = assesses.stream().map(assessTO -> assessTO.getId()).collect(Collectors.toList());
        List<JobseekerAssessRateTO> rateList = rateDao.select(NAMESPACE_RATE, new JobseekerAssessRateQO().setJobseekerAssesIds(assessIds));
        return rateList;
    }

    /**
     * @param login:
     * @return : java.util.List<com.xingkeduo.pojo.table.JobseekerAssessTO>
     * @author : gaozifeng
     * @date : 2019/5/28 10:35
     */
    public List<JobseekerAssessTO> selectByLoginAndType(JobseekerAssessQO query) throws Throwable {
        List<JobseekerAssessTO> assesses = dao.select(NAMESPACE, query);
        for (Iterator<JobseekerAssessTO> iterator = assesses.iterator(); iterator.hasNext(); ) {
            JobseekerAssessTO assess = iterator.next();
            assess.setRates(
                    rateDao.select(NAMESPACE_RATE, new JobseekerAssessRateQO().setAssessId(assess.getId())));
        }
        return assesses;
    }

    /**
     * 查询
     *
     * @param query:
     * @return : java.util.List<com.xingkeduo.pojo.table.JobseekerAssessTO>
     * @author : gaozifeng
     * @date : 2019/5/25 14:56
     */
    public List<JobseekerAssessTO> selectQuery(JobseekerAssessQO query) throws Throwable {
        return dao.select(NAMESPACE, query);
    }

    /**
     * 查询
     *
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年1月2日 下午4:35:01
     */
    public JobseekerAssessTO selectFirstExamByJobseekerIdAndTop(Type type, long jobseekerId, int top) throws Throwable {
        JobseekerAssessQO jobseekerAssessQO = new JobseekerAssessQO().setId(jobseekerId).setType(type).setRecordEnd(top)
                .setRecordStart(0);
        List<JobseekerAssessTO> assesses = dao.select(NAMESPACE, jobseekerAssessQO);

        if (CommonUtil.isEmpty(assesses))
            return null;
        JobseekerAssessTO assess = assesses.get(0);
        assess.setRates(
                rateDao.select(NAMESPACE_RATE, new JobseekerAssessRateQO().setAssessId(assess.getId())));

        return assess;
    }

    /**
     * 新增
     *
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月26日 上午11:47:01
     */
    public void insert(JobseekerAssessTO assess) throws Throwable {
        assess.initId(IdentityUtil.nextId(Worker.USER)).setUpdatedBy(assess.getCreatedBy());
        if (dao.insert(NAMESPACE, assess) < 1)
            throw new DataException(DataException.Type.FAIL, "考核结果");

        for (Iterator<JobseekerAssessRateTO> iterator = assess.getRates().iterator(); iterator.hasNext(); ) {
            JobseekerAssessRateTO rate = iterator.next();
            rate.setJobseekerAssessId(assess.getId());
            rate.initId(IdentityUtil.nextId(Worker.USER)).setCreatedBy(assess.getCreatedBy()).setUpdatedBy(assess.getUpdatedBy());
        }

        if (rateDao.insert(NAMESPACE_RATE, assess.getRates()) < assess.getRates().size())
            throw new DataException(DataException.Type.FAIL, "考核评分");
    }


}
